/*
 * @Description: 表单验证
 * @Autor: 王瑶
 * @Date: 2023-06-07 14:11:06
 * @LastEditors: 王瑶
 * @LastEditTime: 2023-06-08 09:07:30
 */

export default Vue => {
  // 验证规则
  const item = {
    // 非空验证
    required: (msg, trigger) => {
      return {
        required: true,
        message: msg == undefined ? '必填项不能为空' : msg,
        trigger: trigger
      }
    },
    // 数字验证
    number: (msg, trigger) => {
      return {
        type: 'number',
        message: msg == undefined ? '只能输入数字' : msg,
        trigger: trigger
      }
    },
    // boolean 类型
    boolean: (msg, trigger) => {
      return {
        type: 'boolean',
        message: msg == undefined ? '值只能是"true"或"false"' : msg,
        trigger: trigger
      }
    },
    // 整数
    integer: (msg, trigger) => {
      return {
        type: 'integer',
        message: msg == undefined ? '只能输入整数' : msg,
        trigger: trigger
      }
    },
    // 小数
    float: (msg, trigger) => {
      return {
        type: 'float',
        message: msg == undefined ? '只能输入小数' : msg,
        trigger: trigger
      }
    },
    // 时间
    date: (msg, trigger) => {
      return {
        type: 'date',
        message: msg == undefined ? '只能输入时间' : msg,
        trigger: trigger
      }
    },
    // 地址
    url: (msg, trigger) => {
      return {
        type: 'url',
        message: msg == undefined ? '地址格式不正确' : msg,
        trigger: trigger
      }
    },
    // 邮箱
    email: (msg, trigger) => {
      return {
        type: 'email',
        message: msg == undefined ? '邮箱格式不正确' : msg,
        trigger: trigger
      }
    },
    // 验证手机号
    mobile: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^1[3456789]\d{9}$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '手机号格式不正确' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 匹配 汉字，字母，数字
    name: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '用户名只能是，汉字、字母或数字' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 正整数包含0
    int: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[0-9]*[0-9][0-9]*$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '必须是正整数' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 正整数 不包含0
    intNotZero: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[0-9]*[1-9][0-9]*$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '必须是正整数(不包含0)' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 数字 可以是小数
    floatNumber: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^\d+(\.\d+)?$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '必须是数字或者小数' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 密码验证
    password: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{8,}$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '密码至少包含字母、数字、特殊字符中的两种，且长度至少8位' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 账号
    mark: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[a-zA-Z0-9]{1}([a-zA-Z0-9]){2,20}$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '账号必须是,3-20位的字母或数字' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 字母
    letter: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[a-zA-Z]+$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '只能是字母' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 字母 数字 下划线
    used: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^\w+$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '只能是26个英文字母、数字或下划线' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 不能输入汉字
    usedNoChinese: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          callback(new Error(msg == undefined ? '不能输入汉字' : msg))
        } else {
          callback()
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // IP 地址验证
    IP: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? 'IP地址不合法请重新输入' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 数据，可以三位小数（正负）
    threeNumber: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[+-]?\d+(.\d{1,3})?$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '最多保留三位小数' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    },
    // 数据，可以二位小数（正）
    twoNumber: (msg, trigger) => {
      let way = (rule, value, callback) => {
        if (/^[+]?\d+(.\d{1,2})?$/.test(value) || value === undefined || value === null || value === '') {
          callback()
        } else {
          callback(new Error(msg == undefined ? '最多保留二位小数,而不能为负数' : msg))
        }
      }
      return {
        validator: way,
        trigger: trigger
      }
    }
  }

  Vue.prototype.$everTest = (key, val, old) => {
    // 返回值
    let result = []
    if (typeof key === 'string') {
      let keys = key.split('|')
      let vals = typeof val === 'string' ? val.split('|') : []
      for (let i = 0; i < keys.length; i++) {
        let trigger = ''
        let fn = ''
        if (typeof keys[i] === 'string') {
          let names = keys[i].split('.')
          fn = names[0]
          let trig = names[1]
          if (trig == 'all') {
            trigger = ['blur', 'change']
          } else if (trig == 'blur') {
            trigger = 'blur'
          } else if (trig == 'change') {
            trigger = 'change'
          } else {
            trigger = 'change'
          }
        }
        if (typeof item[fn] === 'function') {
          if (vals[i] == undefined || vals[i] == '') {
            vals[i] = undefined
          }
          result.push(item[fn](vals[i], trigger))
        }
      }
    }
    if (key instanceof Array) {
      result.push.apply(result, key)
    }
    if (val instanceof Array) {
      result.push.apply(result, val)
    }
    if (old instanceof Array) {
      result.push.apply(result, old)
    }
    return result
  }
};
